const categoriesMode = require('../model/admin_categoriesModel');
module.exports = {
  addNewCategories(req,res){
    let {name,slug,classname} = req.body;
    let data = {name,slug,classname};
    categoriesMode.addNewCategories(data,(err,result)=>{
      let resObj = {};
      if(err){
        console.error(err);
        resObj.code = 401;
        resObj.msg = '获取失败';
        res.send(resObj);
      }else{
        if(result.affectedRows == 1){
          resObj.code = 200;
          resObj.msg = '添加成功';
          let insertId = result.insertId;
          categoriesMode.getCategoriesById(insertId,(err,result)=>{
            // console.log(result)
            if(err) console.error(err);
            if(result){
              resObj.code = 200;
              resObj.msg = '获取成功';
              resObj.data = result;
            }else{
              resObj.msg = '没有数据';
            }
            res.send(resObj);
          });
        };
      };

    });

  },
  delCategoriesById(req,res){
    console.log(req.query.id);
    categoriesMode.delCategoriesById(req.query.id,(err,result)=>{
      if(err) console.error(err);
      resObj = {
       code: 401,
       msg: '删除失败',
      };
      if(result.affectedRows == 1){
         resObj.code = 200;
         resObj.msg = '删除成功';
      };
      res.send(resObj);
    });
  },
  getcategoiesById(req,res){
    // console.log(req.query.id);
  categoriesMode.getCategoriesById(req.query.id,(err,result)=>{
    // console.log(result)
    if(err) console.error(err);
    let resObj = {
      code : 401,
      msg: '获取失败'
    };
    if(result){
       resObj.code = 200;
       resObj.msg = '获取成功';
       resObj.data = result;
    };
    res.send(resObj);
  })
  },
  editCategories(req,res){
    let {id,name,slug,classname} = req.body;
  categoriesMode.editCategories(id,{name,slug,classname},(err,result)=>{
    if(err) console.error(err);
    let resObj = {
      code : 401,
      msg: '获取失败'
    };
    if(result.affectedRows == 1){
       resObj.code = 200;
       resObj.msg = '获取成功';
      //  resObj.data = result;
      // 发请求到数据库那数据更新
    };
    res.send(resObj);
  });
  },
  deletemultiple(req,res){
    categoriesMode.deletemultiple(req.query.id,(err,result)=>{
      let resObj = {};
      if(err) console.error(err);
      if(result.affectedRows != 0){
         resObj.code = 200;
         resObj.msg = '删除成功';
      }else{
        resObj.code = 401;
        resObj.msg = '删除失败';
      };
      res.send(resObj);
    });
  },
  // 前台
  getNavigation(req,res){
    categoriesMode.getNavigation((err,result)=>{
      if(err) console.error(err);
      let resObj = {
        code : 401,
        msg : '获取失败',
      }
      if(result.length > 0){
        resObj.code = 200;
        resObj.msg = '获取成功';
        resObj.data = result;
      };
      res.send(resObj);
    });
  },
  hideNavigation(req,res){
    categoriesMode.hideNavigation(req.query.id,(err,result)=>{
      if(err) console.error(err);
      let resObj = {
        code : 401,
        msg : '修改失败',
      }
      if(result.affectedRows == 1){
        resObj.code = 200;
        resObj.msg = '修改成功';
      };
      res.send(resObj);
    });

  },
  showNavigation(req,res){
    categoriesMode.showNavigation(req.query.id,(err,result)=>{
      if(err) console.error(err);
      let resObj = {
        code : 401,
        msg : '修改失败',
      }
      if(result.affectedRows == 1){
        resObj.code = 200;
        resObj.msg = '修改成功';
      };
      res.send(resObj);
    });

  }
};